Python One-Liners by Christian Mayer
K-Means Clustering in One Line
If there’s one clustering algorithm you need to know—whether you’re a computer scientist, data scientist, or machine learning expert—it’s the K-Means algorithm. In this section, you’ll learn the general idea and when and how to use it in a single line of Python code.
The Basics
The previous sections covered supervised learning, in which the training data is labeled. In other words, you know the output value of every input value in the training data. But in practice, this isn’t always the case. Often, you’ll find yourself confronted with unlabeled data—especially in many data analytics applications—where it’s not clear what “the optimal output” means. In these situations, a prediction is impossible (because there is no output to start with), but you can still distill useful knowledge from these unlabeled data sets (for example, you can find clusters of similar unlabeled data). Models that use unlabeled data fall under the category of unsupervised learning.
As an example, suppose you’re working in a startup that serves different target markets with various income levels and ages. Your boss tells you to find a certain number of target personas that best fit your target markets. You can use clustering methods to identify the average customer personas that your company serves. Figure 4-10 shows an example.